package com.speedchina.permission.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.speedchina.common.download.base.GetDictMapping;
import com.speedchina.permission.dao.sys.DictItemDao;
import com.speedchina.permission.domain.sys.dto.DictItemDTO;
import com.speedchina.permission.domain.sys.entity.DictItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * @Author xz
 * Data: 2021/5/14
 */
@Component
public class QueryDictItemImpl implements GetDictMapping {
    @Autowired
    private DictItemDao dictItemDao;

    @Override
    public Map<String, String> getDictMapping(String dictType) {
        //根据字典类型查询字典项
        List<DictItemDTO> dictItemDTOList = dictItemDao.selectItemByDictType(dictType);
        HashMap<String, String> resultMap = new HashMap<>();
        for (DictItemDTO dictItemDTO  : dictItemDTOList) {
            //返回值key : 字符代号  value：前端显示值，也就是需要转换的值
            resultMap.put(dictItemDTO.getValue(),dictItemDTO.getLabel());
        }
        return resultMap;
    }

    @Override
    public Map<String, String> getDictMappingReverse(String dictType) {
        List<DictItem> dictItems = dictItemDao.selectList(new LambdaQueryWrapper<DictItem>().eq(DictItem::getDictCode, dictType));
        HashMap<String, String> resultMap = new HashMap<>();
        for (DictItem dictItemDTO  : dictItems) {
            resultMap.put(dictItemDTO.getLabel(),dictItemDTO.getValue());
        }
        return resultMap;
    }
}
